<template>
  <div class="category-container">
    <el-card  class="article-item" shadow="hover"
      v-if="infoList.length > 0">
      <div class="article-content" @click="handleArticleClick(item)" v-for="(item, index) in infoList" :key="index">
        <div class="article-cover" v-if="item.cover">
          <img :src="item.cover" :alt="item.title">
        </div>
        <div class="article-info">
          <h3 class="article-title">{{ item.title }}</h3>
          <p class="article-summary">{{ item.content }}</p>
          <div class="article-footer">
            <span class="article-time">
              <i class="el-icon-time"></i>
              {{ item.createTime }}
            </span>
            <span class="article-views">
              <i class="el-icon-view"></i>
              {{ item.viewNum || 0 }} 阅读
            </span>
            <span class="article-views">
              <i class="el-icon-like"></i>
              {{ item.likeNum || 0 }} 点赞
            </span>
          </div>
        </div>
      </div>
      <div class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
          :page-size="pageSize" :page-sizes="[5, 10, 20, 50]" layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </el-card>
    <el-card v-if="infoList.length == 0">
      <el-empty description="暂无数据"></el-empty>
    </el-card>
  </div>
</template>

<script>
import NewPart from '../home/new-part'
import { reqArticleList } from '@/api'

export default {
  name: 'Category',
  components: {
    NewPart
  },
  data() {
    return {
      loading: false,
      infoList: [],
      pageNum: 1,
      pageSize: 5,
      total: 0
    }
  },
  mounted() {
    this.getArticle();
  },
  methods: {
    getArticle() {
      this.loading = true;
      reqArticleList({
        categoryId: this.$route.query.categoryId
      }).then(res => {
        this.infoList = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      })
    },
    handleArticleClick(article) {
      this.$router.push(`/article/detail/${article.id}`);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getArticle();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getArticle();
    }
  },
  watch: {
    '$route.query.categoryId': {
      handler() {
        this.getArticle();
      },
    }
  }
}
</script>

<style scoped>
.category-container {
  width: 1200px;
  background: #fff;
  border-radius: 8px;
  margin: 0 auto;
  margin-top: 15px;

}

.article-header {
  margin-bottom: 20px;
}

.article-header h2 {
  font-size: 20px;
  color: #303133;
  margin: 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #409EFF;
}

.article-list {
  height: calc(100% - 60px);
  overflow-y: auto;
}

.article-item {
  margin-bottom: 15px;
  cursor: pointer;
  transition: all 0.3s;
}

.article-content {
  padding: 10px;
  display: flex;
  gap: 15px;
}

.article-cover {
  flex-shrink: 0;
  width: 200px;
  height: 120px;
  overflow: hidden;
  border-radius: 4px;
}

.article-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.article-cover img:hover {
  transform: scale(1.05);
}

.article-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.article-title {
  font-size: 16px;
  color: #303133;
  margin: 0 0 10px 0;
  font-weight: 600;
}

.article-summary {
  font-size: 14px;
  color: #606266;
  margin: 0 0 10px 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  flex: 1;
}

.article-footer {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #909399;
}

.article-time,
.article-views {
  margin-right: 15px;
  display: flex;
  align-items: center;
}

.article-time i,
.article-views i {
  margin-right: 4px;
}

/* 自定义滚动条样式 */
.article-list::-webkit-scrollbar {
  width: 6px;
}

.article-list::-webkit-scrollbar-thumb {
  background: #dcdfe6;
  border-radius: 3px;
}

.article-list::-webkit-scrollbar-track {
  background: #f5f7fa;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
</style>